package com.carrental.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carrental.vehicle.entity.ImageCategory;
import com.carrental.vehicle.mapper.ImageCategoryMapper;
import com.carrental.vehicle.service.ImageCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * 图片分类服务实现类
 */
@Service
public class ImageCategoryServiceImpl extends ServiceImpl<ImageCategoryMapper, ImageCategory> implements ImageCategoryService {

    @Override
    public IPage<ImageCategory> pageList(Integer current, Integer size, String name, String code) {
        LambdaQueryWrapper<ImageCategory> queryWrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.hasText(name)) {
            queryWrapper.like(ImageCategory::getName, name);
        }
        
        if (StringUtils.hasText(code)) {
            queryWrapper.eq(ImageCategory::getCode, code);
        }
        
        queryWrapper.orderByAsc(ImageCategory::getSort);
        
        Page<ImageCategory> page = new Page<>(current, size);
        return this.page(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addCategory(ImageCategory imageCategory) {
        imageCategory.setCreateTime(LocalDateTime.now());
        imageCategory.setUpdateTime(LocalDateTime.now());
        return this.save(imageCategory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCategory(ImageCategory imageCategory) {
        imageCategory.setUpdateTime(LocalDateTime.now());
        return this.updateById(imageCategory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCategory(Long id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStatus(Long id, Integer status) {
        ImageCategory category = new ImageCategory();
        category.setId(id);
        category.setStatus(status);
        category.setUpdateTime(LocalDateTime.now());
        return this.updateById(category);
    }
} 